package com.betty.cscams.service.impl;

import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.dao.*;
import com.betty.cscams.entity.*;
import com.betty.cscams.service.BusFlowService;
import com.betty.cscams.util.SecurityUtils;
import com.betty.cscams.vo.BusCommonBean;
import com.betty.cscams.vo.BusFlowBean;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class BusFlowServiceImpl implements BusFlowService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private BusRecordMapper busRecordMapper;
    @Autowired
    private BusSportsMapper busSportsMapper;
    @Autowired
    private BusExpandMapper busExpandMapper;
    @Autowired
    private BusIntellectualMapper busIntellectualMapper;
    @Autowired
    private  BusMoralMapper busMoralMapper;
    @Autowired
    private BusScoreMapper busScoreMapper;
    /**
     * 审核通过
     * @param params
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int auditFlow(BusFlowBean params) {
        SysUser sysUser = sysUserMapper.findByName(SecurityUtils.getUsername());

        List<BusCommonBean> busies = params.getList();
        for (BusCommonBean bean : busies) {
            String operationName = "";
            Integer operationType =0;
            Integer status =0;
            Long toOperationId = 0L;
            SysUser student = sysUserMapper.findByName(bean.getStudentNum());
            if(bean.getStatus() == SysConstants.REVIEW_STATUS_5){
                 operationName = "审核完成";
                 operationType =SysConstants.OPERATION_TYPE_4;
                 status =SysConstants.REVIEW_STATUS_6;
            }else if(bean.getStatus() == SysConstants.REVIEW_STATUS_3 || bean.getStatus() == SysConstants.REVIEW_STATUS_4){
                operationName = "审核通过";
                operationType =SysConstants.OPERATION_TYPE_2;
                status =SysConstants.REVIEW_STATUS_5;
                SysDept dept = sysDeptMapper.findParentByDeptId(student.getDeptId());
                SysUser auditUser = sysUserMapper.selectAuditUser(dept.getId(),1);
                if(ObjectUtils.isEmpty(auditUser)){
                    continue;
                }
                toOperationId = auditUser.getId();
            }else if(bean.getStatus() == SysConstants.REVIEW_STATUS_2 || bean.getStatus() == SysConstants.REVIEW_STATUS_1){
                operationName = "提交审核";
                operationType =SysConstants.OPERATION_TYPE_1;
                status =SysConstants.REVIEW_STATUS_3;
                SysUser auditUser = sysUserMapper.selectAuditUser(student.getDeptId(),3);
                if(ObjectUtils.isEmpty(auditUser)){
                    continue;
                }
                toOperationId = auditUser.getId();
            }
            BusRecord record = new BusRecord();
            record.setBusiId(bean.getId());
            record.setType(params.getType());
            record.setLastUpdateBy(SecurityUtils.getUsername());
            record.setLastUpdateTime(new Date());
            record.setCreateBy(SecurityUtils.getUsername());
            record.setCreateTime(new Date());
            record.setOperationId(sysUser.getId());
            record.setOperationUser(sysUser.getNickName());
            record.setToOperationId(toOperationId);
            record.setToOperationUser(toOperationId==0?"":sysUserMapper.selectByPrimaryKey(toOperationId).getNickName());
            record.setDesc(params.getDesc());
            record.setOperationType(operationType);
            record.setOperationName(operationName);
            record.setStatus(1);

            BusRecord old = new BusRecord();
            old.setBusiId(bean.getId());
            old.setType(params.getType());
            busRecordMapper.finished(old);
            busRecordMapper.add(record);

            if(params.getType() ==2 ){
                BusMoral busMoral = new BusMoral();
                busMoral.setId(bean.getId());
                busMoral.setStatus(status);
                busMoralMapper.update(busMoral);
            }else if(params.getType() ==4){
                BusExpand busExpand = new BusExpand();
                busExpand.setId(bean.getId());
                busExpand.setStatus(status);
                busExpandMapper.update(busExpand);
            }else if(params.getType() ==5 || params.getType() ==3 ){
                BusScore busScore = new BusScore();
                busScore.setId(bean.getId());
                busScore.setStatus(status);
                busScoreMapper.update(busScore);
            }
        }
        return 1;
    }

    /**
     * 退回
     * @param params
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int backFlow(BusFlowBean params) {
        SysUser sysUser = sysUserMapper.findByName(SecurityUtils.getUsername());

        List<BusCommonBean> busies = params.getList();
        for (BusCommonBean bean : busies) {
            String operationName = "退回审核";
            Integer operationType =SysConstants.OPERATION_TYPE_3;
            Integer status =0;
            Long toOperationId = 0L;
            SysUser student = sysUserMapper.findByName(bean.getStudentNum());
            if(bean.getStatus() == SysConstants.REVIEW_STATUS_5){
                status =SysConstants.REVIEW_STATUS_4;
                SysUser auditUser = sysUserMapper.selectAuditUser(student.getDeptId(),3);
                if(ObjectUtils.isEmpty(auditUser)){
                    continue;
                }
                toOperationId = auditUser.getId();
            }else if(bean.getStatus() == SysConstants.REVIEW_STATUS_3 || bean.getStatus() == SysConstants.REVIEW_STATUS_4){
                status =SysConstants.REVIEW_STATUS_2;
                toOperationId = student.getId();
            }
            BusRecord record = new BusRecord();
            record.setBusiId(bean.getId());
            record.setType(params.getType());
            record.setLastUpdateBy(SecurityUtils.getUsername());
            record.setLastUpdateTime(new Date());
            record.setCreateBy(SecurityUtils.getUsername());
            record.setCreateTime(new Date());
            record.setOperationId(sysUser.getId());
            record.setOperationUser(sysUser.getNickName());
            record.setToOperationId(toOperationId);
            record.setToOperationUser(sysUserMapper.selectByPrimaryKey(toOperationId).getNickName());
            record.setDesc(params.getDesc());
            record.setOperationType(operationType);
            record.setOperationName(operationName);
            record.setStatus(1);

            BusRecord old = new BusRecord();
            old.setBusiId(bean.getId());
            old.setType(params.getType());
            busRecordMapper.finished(old);

            busRecordMapper.add(record);

            if(params.getType() ==2 ){
                BusMoral busMoral = new BusMoral();
                busMoral.setId(bean.getId());
                busMoral.setStatus(status);
                busMoralMapper.update(busMoral);
            }else if(params.getType() ==4){
                BusExpand busExpand = new BusExpand();
                busExpand.setId(bean.getId());
                busExpand.setStatus(status);
                busExpandMapper.update(busExpand);
            }else if(params.getType() ==5 || params.getType() ==3){
                BusScore busScore = new BusScore();
                busScore.setId(bean.getId());
                busScore.setStatus(status);
                busScoreMapper.update(busScore);
            }
        }
        return 1;
    }

    /**
     * 审核记录查询
     * @param busiId
     * @param type
     * @return
     */
    @Override
    public List<BusRecord> findList(Long busiId, Integer type) {
        return busRecordMapper.findList(busiId,type);
    }
}
